package com.itqf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/20
 * @Time: 下午2:37
 */
@Controller
@SessionAttributes({"name","user"})//把用Model  .. 存储到request域中的数据复制到session域中
public class MyScopeController {

    @RequestMapping("/testScope") //url+请求方式
    public  String  test1(HttpServletRequest request, HttpSession httpSession){
        request.setAttribute("name","reques中的数据");
        httpSession.setAttribute("username","session域中的数据（username）");

        httpSession.setAttribute("name","session域中的数据");

//        httpSession.invalidate();
//        httpSession.setMaxInactiveInterval(0);
//
        return "forward:/jsp/success.jsp";//跟视图解析器无关
    }

    @RequestMapping("/testScope1") //url+请求方式
    public  String  test1(Model model){
        //存储到request域中
        model.addAttribute("name","reques中的数据(model)");

        //return "forward:/jsp/successs.jsp";//跟视图解析器无关
        return "/jsp/success.jsp";//跟视图解析器无关

    }

    @RequestMapping("/testScope2") //url+请求方式
    public ModelAndView test1(){
        ModelAndView modelAndView = new ModelAndView();
        //存储到request域中
        modelAndView.addObject("name","reques中的数据(ModelAndView)");
        modelAndView.setViewName("forward:/jsp/success.jsp");//路径

        return modelAndView;
    }
    @RequestMapping("/testScope3") //url+请求方式
    public String test3(Map map){
        map.put("name","request中的数据(map)");
        return "/jsp/success.jsp";//转发 重定向  跟视图解析器无关
    }


    @RequestMapping("/cleanSession")
    //清除用@SessionAttributes("name")存到session中的数据
    public  String  clean(SessionStatus sessionStatus){
        sessionStatus.setComplete();//清除用@SessionAttributes("name")存到session中的数据
        //原生session（HttpSession）存储的数据不能清除

        return "/jsp/success.jsp";

    }
}
